<?php

namespace apiend\modules\passport\models;

use Yii;
use common\base\Model;
use apiend\models\forms\SmsCaptchaForm;
use common\models\feedback\FeedbackForget;

/**
 * 忘记密码
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class PasswordRetrieveForm extends Model {

    /**
     * @var integer 密码重置token
     */
    public $type_id = 0;

    /**
     * @var string 短信验证码
     */
    public $username;

    /**
     * @var string 待设置的新密码
     */
    public $idcardno;

    /**
     * @var string 待设置的新密码
     */
    public $photo;

    /**
     * @var string 待设置的新密码
     */
    public $mobile;

    /**
     * @var string 待设置的新密码
     */
    public $remark;

    /**
     * @var string 待设置的新密码
     */
    public $code;

    /**
     * @var User 用户信息
     */
    private $_user = null;

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['username', 'idcardno', 'photo', 'remark', 'code', 'mobile'], 'required'],
            ['username', 'validateUsername'],
            ['code', 'validateCaptcha'],
        ];
    }

    /**
     * 校验用户是否存在
     * @param string $attribute the attribute currently being validated
     */
    public function validateUsername($attribute) {
        if ($this->hasErrors()) {
            return;
        }
        $user = $this->getUser();
        if (!$user) {
            $this->addError($attribute, '您输入的帐号名称不存在');
        }
    }

    /**
     * Finds user by [[username]]
     *
     * @return \api\models\UserCompatibleModel|null
     */
    protected function getUser() {
        if ($this->_user === null) {
            $this->_user = User::findByUsername($this->username);
        }
        return $this->_user;
    }

    /**
     * Validates the password.
     * This method serves as the inline validation for password.
     *
     * @param string $attribute the attribute currently being validated
     * @param array $params the additional name-value pairs given in the rule
     */
    public function validateCaptcha($attribute) {
        if ($this->hasErrors()) {
            return;
        }
        $captcha = Yii::$app->cache->get('captcha_' . SmsCaptchaForm::SCOPE_RETRIEVE . '_' . $this->mobile);
        if ($captcha == false) {
            $this->addError($attribute, $this->getAttributeLabel($attribute) . '已过期');
        } elseif ($captcha !== $this->$attribute) {
            $this->addError($attribute, $this->getAttributeLabel($attribute) . '不正确');
        }
    }

    /**
     * Sends an email with a link, for resetting the password.
     *
     * @return bool whether the email was send
     */
    public function send() {
        if (!$this->validate()) {
            return $this->dumpError();
        }
        $user = $this->getUser();
        if (!$user) {
            return false;
        }
        $model = new FeedbackForget();
        $model->attributes = $this->attributes;
        $model->user_id = $user->id;
        if (!$model->save()) {
            return false;
        }
        Yii::$app->cache->delete('captcha_' . SmsCaptchaForm::SCOPE_RETRIEVE . '_' . $this->mobile);
        return true;
    }

}
